/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nominalitzacions.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.GroupLayout.Alignment;

/**
 *
 * @author Dani.camacho.rodriguez
 */
public class AddPairCorpusView extends JFrame {
    
    private PairCorpusViewController PCVC;
    
    private JPanel mainPanel = new JPanel();
    private JPanel iepPanel = new JPanel();
    private JPanel infPanel = new JPanel();
    private JButton add = new JButton("Add");
    private JTextField insertAlpha = new JTextField();
    private JTextField insertBeta = new JTextField();
    private JLabel alpha = new JLabel("Alpha :");
    private JLabel beta = new JLabel("Beta :");
    
    public AddPairCorpusView(PairCorpusViewController PCVC){
        this.PCVC = PCVC;
        iniComponents();
    }
    
    private void iniComponents() {
        iniFrame();
        iniMainPanel();
        assignListeners();
    }
    
    private void iniFrame(){
        setMinimumSize(new Dimension(350, 110));
        this.setLocationRelativeTo(null);
        this.setPreferredSize(this.getMinimumSize());
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setTitle("Add Pair in Corpus");
        setResizable(false);
        JPanel contentPane = (JPanel) this.getContentPane();
        contentPane.add(mainPanel);
    }
    
    private void iniMainPanel(){
        
        infPanel.setPreferredSize(new Dimension(300,100));
        
        GroupLayout layout = new GroupLayout(mainPanel);
        mainPanel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        GroupLayout.SequentialGroup heightGroup = layout.createSequentialGroup();
        heightGroup.addGroup(layout.createParallelGroup()
            .addComponent(infPanel));
        heightGroup.addGroup(layout.createParallelGroup()
            .addComponent(iepPanel));
        layout.setHorizontalGroup(heightGroup);

        GroupLayout.SequentialGroup verGroup = layout.createSequentialGroup();
        verGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
                .addComponent(infPanel)
                .addComponent(iepPanel));
        layout.setVerticalGroup(verGroup);
        
        
        GroupLayout layout1 = new GroupLayout(infPanel);
        infPanel.setLayout(layout1);
        layout1.setAutoCreateGaps(true);
        layout1.setAutoCreateContainerGaps(true);
        GroupLayout.SequentialGroup heightGroup1 = layout1.createSequentialGroup();
        heightGroup1.addGroup(layout1.createParallelGroup()
            .addComponent(alpha)
            .addComponent(beta));
        heightGroup1.addGroup(layout1.createParallelGroup()
            .addComponent(insertAlpha)
            .addComponent(insertBeta));
        layout1.setHorizontalGroup(heightGroup1);

        GroupLayout.SequentialGroup verGroup1 = layout1.createSequentialGroup();
        verGroup1.addGroup(layout1.createParallelGroup(Alignment.BASELINE)
                .addComponent(alpha)
                .addComponent(insertAlpha));
        verGroup1.addGroup(layout1.createParallelGroup(Alignment.BASELINE)
                .addComponent(beta)
                .addComponent(insertBeta));
        layout1.setVerticalGroup(verGroup1);
        
        iepPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
        add.setPreferredSize(new Dimension(60,55));
        iepPanel.add(add);
    }
    
    private void assignListeners(){
         add.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evento){
                try {
                    addVerb();
                } catch (IOException ex) {
                    String msg = ex.getMessage();
                    JOptionPane.showMessageDialog(new JPanel(), msg, "Error writing",
                                                    JOptionPane.ERROR_MESSAGE);
                }
            }
        });
    }
    
    private void addVerb() throws IOException{
        PCVC.add(insertAlpha.getText(), insertBeta.getText());
        insertAlpha.setText("");
        insertBeta.setText("");
    }

}
